package edu.xhu.behavior.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xhu.api.id.IdClient;
import edu.xhu.behavior.mapper.BiliUserFollowGroupMapper;
import edu.xhu.behavior.mapper.BiliUserFollowMapper;
import edu.xhu.behavior.service.BiliUserFollowGroupService;
import edu.xhu.behavior.util.IdGen;
import edu.xhu.model.behavior.enums.FollowType;
import edu.xhu.model.behavior.pojos.BiliUserFollow;
import edu.xhu.model.behavior.pojos.BiliUserFollowGroup;
import edu.xhu.model.biliuser.pojos.BiliUser;
import edu.xhu.model.common.dtos.ResponseResult;
import edu.xhu.model.common.enums.AppHttpCodeEnum;
import edu.xhu.utils.thread.BiliUserThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class BiliUserFollowGroupServiceImpl extends ServiceImpl<BiliUserFollowGroupMapper, BiliUserFollowGroup> implements BiliUserFollowGroupService {

    private final IdClient idClient;

    private final BiliUserFollowMapper biliUserFollowMapper;

    /**
     * 新建分组
     */
    @Override
    @Transactional
    public ResponseResult<?> addGroup(String groupName) {
        //1.参数校验
        if (groupName == null || groupName.isEmpty() || groupName.length() > 16) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        BiliUserFollowGroup followGroup = getOne(Wrappers.<BiliUserFollowGroup>lambdaQuery().eq(BiliUserFollowGroup::getGroupName, groupName));
        if (followGroup != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        Long id = IdGen.getBehaviorId(idClient);
        if (id == 0L) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "id生成失败");
        }
        BiliUser user = BiliUserThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.new
        BiliUserFollowGroup biliUserFollowGroup = new BiliUserFollowGroup();
        biliUserFollowGroup
                .setGroupId(id)
                .setGroupName(groupName)
                .setUserId(user.getUserId())
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        //3.insert
        if (!save(biliUserFollowGroup)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "新建分组失败！");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode());
    }

    /**
     * 删除分组
     */
    @Override
    @Transactional
    public ResponseResult<?> delGroup(Long groupId) {
        //1.参数校验
        if (groupId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        BiliUserFollowGroup followGroup = getById(groupId);
        if (followGroup == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.查该用户下该分组所有关注信息
        List<BiliUserFollow> follows = biliUserFollowMapper.selectList(Wrappers.<BiliUserFollow>lambdaQuery().eq(BiliUserFollow::getGroupId, groupId));
        if (follows != null && !follows.isEmpty()) {
            //该组下,有关注的用户,批量更新
            List<Long> ids = follows.stream().map(BiliUserFollow::getId).collect(Collectors.toList());
            if (biliUserFollowMapper.updateBatchById(ids, null, FollowType.DEFAULT.getCode()) != ids.size()) {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "关注表更新失败！");
            }
        }
        //3.删除分组
        if (!removeById(groupId)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"删除分组失败！");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改分组名称
     */
    @Override
    @Transactional
    public ResponseResult<?> updateGroup(Long groupId, String groupName) {
        //1.参数校验
        if (groupId == null || groupName == null || groupName.isEmpty() || groupName.length() > 16) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        BiliUserFollowGroup followGroup = getById(groupId);
        if (followGroup == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.set
        followGroup.setGroupName(groupName);
        //3.update
        if (!updateById(followGroup)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "修改分组失败！");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
